package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 分类品牌 Service 实现类
 *
 * @author Taeyang
 * @since 2025-04-17
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询分类品牌列表
     *
     * @param categoryBrand 分类品牌
     * @return 分类品牌集合
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        return categoryBrandMapper.selectCategoryBrandList(categoryBrand);
    }

    /**
     * 获取分类品牌详细信息
     *
     * @param id 分类品牌 id
     * @return 分类品牌信息
     */
    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        // 根据分类品牌id查询分类品牌基本信息
        CategoryBrand categoryBrand = baseMapper.selectById(id);

        // 调用CategoryService层的方法获取分片品牌本级及所有父级的id
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());

        // 补充查询到的基本信息的categoryIdList字段
        categoryBrand.setCategoryIdList(categoryIdList);

        return categoryBrand;
    }

    /**
     * 新增分类品牌
     *
     * @param categoryBrand 分类品牌
     * @return 结果
     */
    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        long count = count(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
        );
        if (count > 0) {
            throw new ServiceException("该分类已添加该品牌");
        }
        return save(categoryBrand) ? 1 : 0;
    }

    /**
     * 修改分类品牌
     *
     * @param categoryBrand 分类品牌
     * @return 结果
     */
    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        // 根据分类品牌id获取现在的分类品牌信息
        CategoryBrand originalCategoryBrand = getById(categoryBrand.getId());

        // 判断是否更改了分类对象id或者品牌对象id
        if (categoryBrand.getCategoryId().longValue() != originalCategoryBrand.getCategoryId().longValue()
                || categoryBrand.getBrandId().longValue() != originalCategoryBrand.getBrandId().longValue()
        ) {
            // 查找表中是否存在与将要修改的分类品牌冲突的数据
            long count = count(new LambdaQueryWrapper<CategoryBrand>()
                    .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                    .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
            );
            if (count > 0) {
                throw new ServiceException("该分类已添加该品牌");
            }
        }
        return categoryBrandMapper.updateById(categoryBrand);
    }

    /**
     * 删除分类品牌
     *
     * @param ids 要删除的id集合
     * @return 结果
     */
    @Override
    public int deleteProductUnitByIds(Long... ids) {
        return categoryBrandMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据分类id获取品牌列表
     *
     * @param categoryId 分类id
     * @return 结果
     */
    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        return categoryBrandMapper.selectBrandListByCategoryId(categoryId);
    }
}
